// xc_lshop/api/jtklm/meituan/meituan.js
// var actbill = require('../../../../xc/model/jtklm')
var actbill = require('../../model/extens.js')

var app = getApp();

Page({
  mixins: [require('../../../../xc/mixins/basicMixin')],
  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: app.globalData.CustomBar,
    navval: 0,
    nav: [{
        val: "1",
        name: "外卖"
      },
      {
        val: "2",
        name: "商超"
      },
      {
        val: "3",
        name: "优选"
      },
      {
        val: "4",
        name: "酒店"
      }
      // {val:"4",name:"优选1.99促销页"},
      // {val:"5",name:"优选秒杀栏目活动"},
      // {val:"7",name:"美团优选新人活动页"},
    ],
    basicurl: "-1",
    list: {},
    text: {
      1: "餐前福利！外卖红包天天领！最高可得66元！",
      2: "低价好货！商超红包天天领！最高可得10元！",
      3: "团购好货！限时1分钱买菜！尽在美团优选！",
      6: "出行无忧！酒店红包天天领！最高可得50元！",
      10: "周末特价！特价红包天天领！最高可得20元！",
    },
    mtui:null
  },
  xcloaddata: function (options, config) {
      

  },
  setconfig(config) {
    if (config['smapiconfig']) {
      if (config['smapiconfig']['basic']['status'] == -1) {

        this.pageback()
      } else {
        var mtui=config['smapiconfig']['meituan']['ui']['editableTabs'];
        // console.log("mtui")
        // console.log(mtui)
        var newmtui=[]
        // var navval=-1;
        for(var i in mtui){
          if(mtui[i]['status']==1){
            newmtui.push(mtui[i])
          }
        }
        this.setData({
          // navval:navval,
          mtui:newmtui
        })
      }
    } else {
      this.pageback()
    }
   

  },
  afterlogin: function (options, config, userinfo) {
    this.setconfig(config)

    var that = this;
    this.setData({
      member: userinfo
    })
    if(options.index){
      this.setData({
       navval:options.index * 1
      })
    }
    if (userinfo['member_id'] != 0 && userinfo['memberinfo']) {
      var index = this.data.navval;
     
     if(this.data.mtui[index]){
      var actid = this.data.mtui[index]['actid'];
      var linktype = this.data.mtui[index]['linktype'];



      this.geturl(actid,linktype)

     }
      // console.log(actid)

    }
  },
  // 登录注册
  login(e) {
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      that.setData({
        "member": app.globalData.member
      })
    })
  },
 
  shareposter(e) {
    var actid= e.currentTarget.dataset.actid
    var linktype = e.currentTarget.dataset.linktype;

    var that=this
    // var mtui = this.data.mtui;

    if (!this.data.list[actid]) {
      app.xc_sys_config.authop.oppower("login").then(function (res) {
      
        if(res['status']==1){
          that.setData({
            member:res['obj']
          })
          app.globalData.member=res['obj']
          that.geturl(actid,linktype, 'showposter')

        }
      })
    } else {
      this.makeposter()
    }

  },
  makeposter(miniProgramQrCode){
    var xcposter = this.selectComponent("#xcposter");
    var mtui = this.data.mtui;
    var type = this.data.navval;
    var actid = mtui[type]['actid']; 

    var bgimg = mtui[type]['poster']; //"http://md.cohooo.net/attachment/images/2/2021/10/PsmAyyuxmzA4YTwmS7XK4y7979XfI9.png"

    var qrimg =""
    if(miniProgramQrCode){
      qrimg=miniProgramQrCode
    }else if(this.data.list[actid] && this.data.list[actid]['miniProgramQrCode']){
      qrimg=this.data.list[actid]['miniProgramQrCode']; //"http://p0.meituan.net/poiqrcode/74062c815334f1f98f369fd8e6657d9a100811.jpg"
    }
  
    xcposter.init(type, bgimg, qrimg)
  },
  onClick(e) {
    console.log("onClick")
    console.log(e)
    this.setData({
      navval: e.detail.name,
    })
    
    var type = e.detail.name
    var actid = this.data.mtui[type].actid
    var linktype = this.data.mtui[type].linktype

    console.log("dddddddd")
    console.log(type)
    console.log(actid)

    this.changedval(actid,linktype)
  },
  onChange(e) {
    console.log("onChange")
    console.log(e)
    this.setData({
      navval: e.detail.name,
    })
    var type = e.detail.name
    var actid = this.data.mtui[type].actid
    var linktype = this.data.mtui[type].linktype
    console.log("ddd")

    console.log(type)

    // console.log(this.data.mtui[type])
    console.log(actid)
    console.log(linktype)

    this.changedval(actid,linktype)
  },
  changedval(actid,linktype){
    if (this.data.member['member_id'] != 0 && !this.data.list[actid]) {
      this.geturl(actid,linktype)
    }
  },
  adnav(e){
    var dataset = e.currentTarget.dataset
    var item=dataset.item;
    console.log("ggggg")
    console.log(dataset)
    if(item['cid']==1){
      wx.navigateTo({
        url: item['url'],
      })
    }else if(item['cid']==10){
       this.navtoactid(item.actid,item.linktype)
    }
  },
  gotonav(e) {
    console.log("点伙凌红好评")
    console.log(e)
    var dataset = e.currentTarget.dataset
    var actid = dataset.actid
    var linktype = dataset.linktype
  
    this.navtoactid(actid,linktype)
    // var type= this.data.navval
  },
  navtoactid(actid,linktype){
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
    
      if(res['status']==1){
        that.setData({
          member:res['obj']
        })
        app.globalData.member=res['obj']
    
          var item = that.data.list[actid];
          console.log("item")
          console.log(item)
          if (item) {
            var app_path = item['path']
       
            that.navtoxcx(app_path,linktype)
          } else {
             

            that.geturl(actid,linktype, 'tonav')
          }
        }
     
     
    })
  },
  navtoxcx(path,linktype) {
    // var appid = "wxde8ac0a21135c07d";
    // if (this.data.navval == 3) {
    //   appid = "wx77af438b3505c00e"
    // }
    // console.log(this.data.mtui)
    // console.log(this.data.navval)
    // if(!appid){
    //   appid=this.data.mtui[this.data.navval].xcx
    // }
    var appid="wxde8ac0a21135c07d"
    if(linktype==8){
      appid = "wx77af438b3505c00e"
    }
    console.log(appid)
    console.log(path)

    wx.navigateToMiniProgram({
      appId: appid,
      path: path,
      fail(e) {
        console.log(e)
      }
    })
  },
  geturl(actid,linktype, op = '') {


    var that = this;
    actbill.geturl(actid,linktype).then(function (res) {
     
      if (res['status'] == 1) {
        var name = 'list[' + actid + ']';
        that.setData({
          [name]: res['obj']
        })
        if (op == "tonav") {
          var app_path = res['obj']['path']

          that.navtoxcx(app_path,linktype)
        } else if (op == "showposter") {
          that.makeposter(res['obj']['miniProgramQrCode'])
        }

        // console.log(that.data.list)
      } else {
        wx.showModal({
          title: "错误",
          content: res['message'] ? res['message'] : '获取失败'
        })
      }

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


})